﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Substring
{
    internal static class StringExtension
    {
        /// <summary>
        /// Insert a substring in another string
        /// </summary>
        /// <param name="substring">The substring value that will be inserted to the original string</param>
        /// <param name="index">Position where we want to insert the substring value</param>
        /// <returns>A new string with the original string plus the substring inserted at the position <para>substring</para></returns>
        public static string InsertSubString(this string input, string substring, int index)
        {
            string temp = string.Empty;

            for (int i = 0; i < input.Length; i++)
            {
                if (i == index)
                    temp += substring + input[i];
                else
                    temp += input[i];
            }

            return temp;
        }
        /// <summary>
        /// Remove all the instaces of the substring in the input string
        /// </summary>
        /// <param name="input">Input string</param>
        /// <param name="substring">substring that will be removed from the <para>input</para></param>
        /// <returns>The input string without the substring</returns>
        public static string RemoveSubstring(this string input, string substring)
        {
            string temp = string.Empty;

            for (int i = 0; i < input.Length; i++)
            {
                int j = 0;
                int k = i;
                bool match = true;
                while (j < substring.Length)
                {
                    if (substring[j] != input[k + j])
                    {
                        match = false;
                        break;

                    }
                    else
                    {
                        i++;
                        //k++;
                        j++;
                    }
                }
                if (!match)
                {
                    temp += input[i];
                }
            }

            return temp;
        }
        /// <summary>
        /// Get the string between <para>begin</para> and <para>end</para>
        /// </summary>
        /// <param name="input">Input String</param>
        /// <param name="begin">Begin Index</param>
        /// <param name="end">End Index</param>
        /// <returns>The string between begin and end index</returns>
        public static string GetSubString(this string input, int begin, int length)
        {
            if (input.Length <= 0
                && begin >= 0
                && begin <= length
                && begin <= input.Length
                )
                return string.Empty;

            string temp = string.Empty;
            int i = length;
            while (i >= 0 && begin < input.Length)
            {
                temp += input[begin];
                begin++;
                i--;
            }

            return temp;
        }
        /// <summary>
        /// Replace the <para>oldString</para> with the <para>newString</para> in the <para>input</para> string.
        /// </summary>
        /// <param name="input">Input String</param>
        /// <param name="oldString">String value to be removed</param>
        /// <param name="newString">String with the new string value</param>
        /// <returns>A string with the <para>newString</para> value instead of <para>oldString</para> value.</returns>
        public static string ReplaceSubString(this string input, string oldString, string newString)
        {
            if (input.Length <= 0)
                return string.Empty;

            string temp = string.Empty;
            for (int i = 0; i < input.Length; i++)
            {
                bool match = true;

                int k = i;
                int j = 0;
                while (j < oldString.Length)
                {
                    if (oldString[j] == input[j + i])
                    {
                        j++;
                        k++;
                    }
                    else
                    {
                        match = false;
                        break;
                    }
                }

                if (match)
                {
                    i = k - 1;
                    temp += newString;
                }
                else
                {
                    temp += input[i];
                }
            }

            return temp;
        }
        /// <summary>
        /// Verify if the <para>substring</para> is a substring of <para>input</para>
        /// </summary>
        /// <param name="input">Input String</param>
        /// <param name="substring">Substring to match</param>
        /// <returns>True is <para>substring</para> is a substring; false in other case</returns>
        public static bool IsSubString(this string input, string substring)
        {
            if (input.Length < substring.Length || input.Length <= 0 || substring.Length <= 0)
                return false;

            bool match = true;

            for (int i = 0; i < input.Length - substring.Length + 1; i++)
            {
                match = true;

                for (int j = 0; j < substring.Length; j++)
                {                    
                    if (input[i] != substring[j])
                    {
                        match = false;
                        break;
                    }

                    i++;
                }

                if (match)
                    break;
            }

            return match;
        }     
    }
}
